import re

from rest_framework import generics
from rest_framework import serializers
from apps.users.models import User

# 查询用户信息序列化器
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model =User
        fields = ("id","username","mobile","email")
# 新增用户信息序列化器
class UserAddSerializer(serializers.ModelSerializer):
    class Meta:
        model =User
        fields = ("id","username","mobile","email")
        extra_kwargs = {
            'username': {'max_length': 20, 'min_length': 5},
             'password': {'max_length': 20, 'min_length': 8, 'write_only': True}
        }
        # 重写create方法
        def create(self, validated_data):
            # 保存用户数据并对密码加密
            user = User.objects.create_user(**validated_data)
            return user
        # 验证用户名
        # 验证密码
        # 验证手机号
        # def validate_username(self,value):
        #     if not re.match(r"[a-zA-Z0-9]",value):
        #         raise serializers.ValidationError()

